package com.imay.live.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;
import com.imay.live.helper.LevelHelper;
import com.imay.live.proto.user.UserInfoCard;
import com.imay.live.utils.GlideUtil;
import com.imay.live.view.recyclerview.WrapContentLinearLayoutManager;

import java.util.Collection;
import java.util.List;

/**
 * Created by jeaman on 2017/4/2.
 * 房间用户列表
 */

public class RoomUserAdapter extends BaseQuickAdapter<UserInfoCard, BaseViewHolder> {
    private int mPageSize = 10;   //每页数量
    private boolean mLoadMoreEnable = false;
    private boolean mLoading = false;
    private int mAutoLoadMoreSize = 1;
    private RequestLoadMoreListener mRequestLoadMoreListener;

    public RoomUserAdapter(List<UserInfoCard> data) {
        super(R.layout.item_room_user, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, UserInfoCard item) {
        int levelRes = LevelHelper.getUserBadgeResId(item.OfficialVip, item.LargeVipStatus, item.UserLv);
        helper.setImageResource(R.id.live_room_iv_level, levelRes);
        GlideUtil.loadCircleBorderImage((ImageView) helper.getView(R.id.item_room_iv_avatar),
                item.imgHead,
                R.dimen.border_avatar,
                R.color.line_ff,
                R.mipmap.pnl_00_head108);
    }

    public void setPageSize(int pageSize) {
        mPageSize = pageSize;
    }

    @Override
    public void setOnLoadMoreListener(RequestLoadMoreListener requestLoadMoreListener, RecyclerView recyclerView) {
        this.mRequestLoadMoreListener = requestLoadMoreListener;
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                WrapContentLinearLayoutManager layoutManager = (WrapContentLinearLayoutManager) recyclerView.getLayoutManager();
                autoLoadMore(layoutManager.findLastVisibleItemPosition()); // 自动加载
            }
        });
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int positions) {
        //autoLoadMore(positions);
        super.onBindViewHolder(holder, positions);
    }

    @Override
    public void setNewData(List<UserInfoCard> data) {
        super.setNewData(data);
        toggleLoadMoreEnable();
    }

    @Override
    public void addData(@NonNull Collection<? extends UserInfoCard> newData) {
        super.addData(newData);
        toggleLoadMoreEnable();
    }

    private void toggleLoadMoreEnable() {
        if (getItemCount() >= mPageSize) {
            mLoadMoreEnable = true;
        } else {
            mLoadMoreEnable = false;
        }
        mLoading = false;
    }

    public void setLoadMoreFailed() {
        mLoading = false;
    }

    public int getPage() {
        int page = getItemCount() / mPageSize;
        if (getItemCount() % mPageSize != 0) {
            page += 1;
        }
        return page;
    }

    private void autoLoadMore(int position) {
        if (position < getData().size() - 1) {
            return;
        }
        if (!mLoadMoreEnable || mLoading || mRequestLoadMoreListener == null || position < getItemCount() - mAutoLoadMoreSize) {
            return;
        }
        mLoading = true;
        if (getRecyclerView() != null) {
            getRecyclerView().post(new Runnable() {
                @Override
                public void run() {
                    mRequestLoadMoreListener.onLoadMoreRequested();
                }
            });
        } else {
            mRequestLoadMoreListener.onLoadMoreRequested();
        }
    }
}
